package com.tebiecloud.order.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tebiecloud.common.constants.ResultEnum;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.order.client.model.entity.Orderr;
import com.tebiecloud.order.client.model.entity.OrderGoods;
import com.tebiecloud.order.client.model.vo.IncomeList;
import com.tebiecloud.order.client.model.vo.IncomeSubList;
import com.tebiecloud.order.client.model.vo.NotArrivedList;
import com.tebiecloud.order.server.mapper.OrderGoodsMapper;
import com.tebiecloud.order.server.mapper.OrderrMapper;
import com.tebiecloud.order.server.service.OrderrService;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import com.tebiecloud.order.server.service.feign.StoreServiceClient;
import com.tebiecloud.store.client.model.entity.Store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单 服务实现类
 * @author tebie
 * @date 2019-09-15
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrderrServiceImpl extends BaseServiceImpl<OrderrMapper, Orderr> implements OrderrService {

    @Autowired
    OrderrMapper orderMapper;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Autowired
    StoreServiceClient storeServiceClient;

    @Override
    public Orderr getLastOne(long userId) {
        QueryWrapper<Orderr> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.isNotNull("phone");
        queryWrapper.orderByDesc("ct");
        queryWrapper.last("limit 1");
        return super.getOne(queryWrapper,false);
    }

    @Override
    public List<NotArrivedList> selectNotArrivedList(Long userId,List<Long> orderIdList) {
        List<NotArrivedList> notArrivedLists = new ArrayList<>();
        QueryWrapper<Orderr> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.in("order_id",orderIdList);
        List<Orderr> orders = orderMapper.selectList(orderQueryWrapper);
        for(Orderr order:orders){
            NotArrivedList notArrivedList = new NotArrivedList();
            notArrivedList.setActName(order.getActName());
            notArrivedList.setActPic(order.getActPic());
            notArrivedList.setCt(order.ct);
            notArrivedList.setOrderId(order.getOrderId());
            notArrivedList.setPrice(order.getPrice());
            if(order.getUserId().equals(userId)){
                notArrivedList.setSelfPurchase(1);
            }else{
                notArrivedList.setSelfPurchase(0);
            }
            //赚多少
            notArrivedList.setProfit(0);
            notArrivedList.setStatus(order.getStatus());
            //获取订单商品
            QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
            orderGoodsQueryWrapper.eq("order_id",order.getOrderId());
            List<OrderGoods> orderGoods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
            if(orderGoods.size()>0){
                //获取门店
                ResultBody<Store> storeResultBody = storeServiceClient.getStore(orderGoods.get(0).getStoreId());
                if(storeResultBody!=null&&storeResultBody.getCode()== ResultEnum.OK.getCode())
                    notArrivedList.setStoreName(storeResultBody.getData().getName());
            }else{
                notArrivedList.setStoreName("暂无门店");
            }
            notArrivedLists.add(notArrivedList);
        }
        return notArrivedLists;
    }

    @Override
    public IncomeList selectIncomeList(List<Long> orderIdList,String month,String day) {
        int orderNumber = 0;//订单总数
        int waitArrivedNum = 0; //待到店订单数
        int arrivedNum = 0; //已到店订单数
        int refundNum = 0; //退款订单数
        int totalProfit = 0; //总收益
        int totalPrice = 0; //交易额
        IncomeList incomeList  = new IncomeList();
        List<IncomeSubList> incomeSubLists = orderMapper.selectIncomeSubList(orderIdList,month,day);
        orderNumber = incomeSubLists.size();
       for(IncomeSubList incomeSubList:incomeSubLists){
           totalProfit = totalProfit + incomeSubList.getProfit();
           totalPrice = totalPrice + incomeSubList.getPrice();
       }
       //查询退款单数
        QueryWrapper<Orderr> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.in("order_id",orderIdList);
        orderQueryWrapper.eq("status",6);
        refundNum = orderMapper.selectCount(orderQueryWrapper);
        //查询已完成订单
        orderQueryWrapper.eq("status",4);
        arrivedNum = orderMapper.selectCount(orderQueryWrapper);
        waitArrivedNum = orderNumber - arrivedNum - refundNum;
        incomeList.setOrderNumber(orderNumber);
        incomeList.setArrivedNum(arrivedNum);
        incomeList.setRefundNum(refundNum);
        incomeList.setWaitArrivedNum(waitArrivedNum);
        incomeList.setTotalPrice(totalPrice);
        incomeList.setTotalProfit(totalProfit);
        incomeList.setRecords(incomeSubLists);
        return incomeList;
    }
}
